﻿using System;
using System.Collections.Generic;
using System.Text;
using DotNet.Model;
using DotNet.Common;
using DotNet.Data.Factory;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
namespace DotNet.Data
{
    public class WebconfigDal
    {
        IDBHelp db = DBHelp.Create();
        Hashtable htSyn = Hashtable.Synchronized(new Hashtable());
        StringBuilder strSql = new StringBuilder();
        #region 是否存在该记录
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int webId)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("select count(1) from Webconfig where webId=@webId");
            htSyn.Add("webId", webId);
            return db.IsExists(strSql.ToString(), htSyn);
        }
        #endregion

        #region 获得网站配置信息数据列表
        /// <summary>
        /// 获得网站配置信息数据列表
        /// </summary>
        /// <returns></returns>
        public IList<Webconfig> GetModel()
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("select webId,featuredProductsNum,hotProductsNum,newProductsNum,randomProductsNum,newNewsNum,productsListNum,NewsNum,Skins,IndexProductsNum,webCount,webKey,Copyright from Webconfig ");
            return getWebconfigsBySql(strSql.ToString());
        }
        #endregion

        #region 根据网站配置信息号查询实体
        /// <summary>
        /// 根据网站配置信息号查询实体
        /// </summary>
        /// <param name="webId"></param>
        /// <returns></returns>
        public Webconfig GetModel(int webId)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("select webId,featuredProductsNum,hotProductsNum,newProductsNum,randomProductsNum,newNewsNum,productsListNum,NewsNum,Skins,IndexProductsNum,webName,webKey from Webconfig  ");
            strSql.Append(" where webId=@webId ");
            htSyn.Add("webId", webId);
            DataTable dt = db.GetDataTable(strSql.ToString(), htSyn);
            Webconfig Webconfig = new Webconfig();
            if (dt.Rows.Count>0)
            {
                Webconfig.webId = Convert.ToInt32(dt.Rows[0]["webId"]);
                Webconfig.featuredProductsNum = Convert.ToInt32(dt.Rows[0]["featuredProductsNum"]);
                Webconfig.hotProductsNum = Convert.ToInt32(dt.Rows[0]["hotProductsNum"]);
                Webconfig.newProductsNum = Convert.ToInt32(dt.Rows[0]["newProductsNum"]);
                Webconfig.randomProductsNum = Convert.ToInt32(dt.Rows[0]["randomProductsNum"]);
                Webconfig.newNewsNum = Convert.ToInt32(dt.Rows[0]["newNewsNum"]);
                Webconfig.productsListNum = Convert.ToInt32(dt.Rows[0]["productsListNum"]);
                Webconfig.newNewsNum = Convert.ToInt32(dt.Rows[0]["newNewsNum"]);
                Webconfig.NewsNum = Convert.ToInt32(dt.Rows[0]["NewsNum"]);
                Webconfig.Skins = dt.Rows[0]["Skins"].ToString();
                Webconfig.IndexProductsNum = Convert.ToInt32(dt.Rows[0]["IndexProductsNum"]);
                Webconfig.webCount = dt.Rows[0]["webCount"].ToString();
                Webconfig.webKey = dt.Rows[0]["webKey"].ToString();
                Webconfig.Copyright = dt.Rows[0]["Copyright"].ToString();
            }
            return Webconfig;
        }
        #endregion

        #region 修改网站配置信息
        /// <summary>
        /// 修改网站配置信息
        /// </summary>
        /// <param name="Webconfig"></param>
        /// <returns></returns>
        public bool Update(Webconfig Webconfig)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("update Webconfig set ");
            strSql.Append("featuredProductsNum=@featuredProductsNum,");
            strSql.Append("hotProductsNum=@hotProductsNum,");
            strSql.Append("newProductsNum=@newProductsNum,");
            strSql.Append("randomProductsNum=@randomProductsNum,");
            strSql.Append("newNewsNum=@newNewsNum,");
            strSql.Append("productsListNum=@productsListNum,");
            strSql.Append("NewsNum=@NewsNum,");
            strSql.Append("Skins=@Skins,");
          
            strSql.Append("IndexProductsNum=@IndexProductsNum,");
            strSql.Append("webCount=@webCount,");
            strSql.Append("webKey=@webKey, ");
            strSql.Append("Copyright=@Copyright");
            strSql.Append(" where webId=@webId ");
            htSyn.Add("@featuredProductsNum", Webconfig.featuredProductsNum);
            htSyn.Add("@hotProductsNum", Webconfig.hotProductsNum);
            htSyn.Add("@newProductsNum", Webconfig.newProductsNum);
            htSyn.Add("@randomProductsNum", Webconfig.randomProductsNum);
            htSyn.Add("@newNewsNum", Webconfig.newNewsNum);
            htSyn.Add("@productsListNum", Webconfig.productsListNum);
            htSyn.Add("@NewsNum", Webconfig.NewsNum);
            htSyn.Add("@Skins", Webconfig.Skins);
           
            htSyn.Add("@IndexProductsNum", Webconfig.IndexProductsNum);
            htSyn.Add("@webCount", Webconfig.webCount);
            htSyn.Add("@webKey", Webconfig.webKey);
            htSyn.Add("@webId", Webconfig.webId);
            htSyn.Add("@Copyright",Webconfig.Copyright);
            if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 删除网站配置信息根据网站配置信息号
        /// <summary>
        /// 删除网站配置信息根据网站配置信息号
        /// </summary>
        /// <param name="webId"></param>
        /// <returns></returns>
        public bool Delete(int webId)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("delete from Webconfig ");
            strSql.Append(" where webId=@webId ");
            htSyn.Add("webId", webId);
            if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 添加网站配置信息
        /// <summary>
        /// 添加网站配置信息
        /// </summary>
        /// <param name="Webconfig"></param>
        /// <returns></returns>
        public bool Insert(Webconfig Webconfig)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("insert into Webconfig(");
            strSql.Append("featuredProductsNum,hotProductsNum,newProductsNum,randomProductsNum,newNewsNum,productsListNum,NewsNum,Skins,IndexProductsNum,webCount,webKey,Copyright)");
            strSql.Append(" values (");
            strSql.Append("@featuredProductsNum,@hotProductsNum,@newProductsNum,@randomProductsNum,@newNewsNum,@productsListNum,@NewsNum,@Skins,@IndexProductsNum,@webCount,@webKey,@Copyright)"); 
            htSyn.Add("@featuredProductsNum", Webconfig.featuredProductsNum);
            htSyn.Add("@hotProductsNum", Webconfig.hotProductsNum);
            htSyn.Add("@newProductsNum", Webconfig.newProductsNum);
            htSyn.Add("@randomProductsNum", Webconfig.randomProductsNum);
            htSyn.Add("@newNewsNum", Webconfig.newNewsNum);
            htSyn.Add("@productsListNum", Webconfig.productsListNum);
            htSyn.Add("@NewsNum", Webconfig.NewsNum);
            htSyn.Add("@Skins", Webconfig.Skins);
            htSyn.Add("@IndexProductsNum", Webconfig.IndexProductsNum);
            htSyn.Add("@webCount", Webconfig.webCount);
            htSyn.Add("@webKey", Webconfig.webKey);
            htSyn.Add("@Copyright", Webconfig.Copyright);
            if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 根据SQL语句获取集合
        /// <summary>
        ///根据SQL语句获取集合
        /// </summary>
        public IList<Webconfig> getWebconfigsBySql(string strSql)
        {
            IList<Webconfig> list = new List<Webconfig>();
            DataTable dt = db.GetDataTable(strSql);
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    Webconfig Webconfig = new Webconfig();
                    Webconfig.webId = Convert.ToInt32(dr["webId"]);
                    Webconfig.featuredProductsNum = Convert.ToInt32(dr["featuredProductsNum"]);
                    Webconfig.hotProductsNum = Convert.ToInt32(dr["hotProductsNum"]);
                    Webconfig.newProductsNum = Convert.ToInt32(dr["newProductsNum"]);
                    Webconfig.randomProductsNum = Convert.ToInt32(dr["randomProductsNum"]);
                    Webconfig.newNewsNum = Convert.ToInt32(dr["newNewsNum"]);
                    Webconfig.productsListNum = Convert.ToInt32(dr["productsListNum"]);
                    Webconfig.newNewsNum = Convert.ToInt32(dr["newNewsNum"]);
                    Webconfig.NewsNum =Convert.ToInt32(dr["NewsNum"].ToString());
                    Webconfig.Skins = dr["Skins"].ToString();
                    Webconfig.IndexProductsNum =Convert.ToInt32(dr["IndexProductsNum"]);
                    Webconfig.webCount = dr["webCount"].ToString();
                    Webconfig.webKey = dr["webKey"].ToString();
                    Webconfig.Copyright = dr["Copyright"].ToString();
                    list.Add(Webconfig);
                }
            }
            return list;
        }
        #endregion
    }
}
